/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1/commercial
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may use this file in compliance with the License.
 * Alternatively, if you can't copy with the License, namely if you do not
 * want to provide source code of your modifications to this file, you may
 * negotiate a commercial licence with the initial developer.
 * You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is COID.
 *
 * The Initial Developer of the Original Code is
 * PosAm.
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 * Brano Kemen
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

#ifndef __COID_SERVER_SERVICE__HEADER_FILE__
#define __COID_SERVER_SERVICE__HEADER_FILE__

#include "serverface.h"
#include "coid/svc/instance.h"
#include "coid/comm/dir.h"

namespace coid {


////////////////////////////////////////////////////////////////////////////////
struct AcceptSvcCbk
{
    virtual ~AcceptSvcCbk() {}
    
    virtual opcd accept( ServiceDescriptor& d, charstr& wd ) const = 0;
};


struct ServiceInstanceCoid;

////////////////////////////////////////////////////////////////////////////////
///Plugin service
struct ServiceCoid : CoidNode
{
    uint                _scit;      ///<scan iteration (psscan)
    ServiceDescriptor   _desc;      ///<service initialization method
//    uint                _autosvc;       ///<BN_SVCI created as autospawn service
    bool                _sorted;

    ServiceCoid( const ServiceDescriptor& hdr, charstr& path ) : _desc(hdr)
    {
        _sorted = false;
        _scit = 0;
        //_name = hdr._name;
        charstr buf;
        _desc._version.get_version(buf,true);
        //_name << ' ' << buf << " service class";
        _path.takeover(path);
        directory::treat_trailing_separator( _path, true );
		_mutex.set_name( "ServiceCoid" );
    }

    virtual token short_name() const        { static token _T("service"); return _T; }

    virtual opcd initialize()
    {
        charstr ver;
        charstr name = _desc._name;
        name << " " << _desc._version.get_version(ver);
		_mutex.set_name( name.ptr() );
        set_name(name);
        return 0;
    }

    opcd spawn_auto_instance();
    opcd spawn_instance( binstream& arg );

    opcd instantiate( void** obj, ServiceInstanceCoid* svcin );


    IMPLEMENTS_VIRTUAL_INTERFACE_ttree_ifc_BEGIN_PURE(ServiceCoid,"0.0",CoidNode);
    IMPLEMENTS_VIRTUAL_INTERFACE_ttree_ifc_END;

    virtual NodeClass GET_NODECLASS()
    {
        return SINGLETON(ClassRegister<ttree_ifc>).find_or_create( GET_INTERFACE().get_iface(), this );
    }
    
    //operator token () const                 { return _desc._name; }
    //operator const version& () const        { return _desc._version; }
    token get_class_name(const void*p) const    { return _desc._name; }
    const version& get_version(const void*p) const  { return _desc._version; }
};


} // namespace coid

#endif //__COID_SERVER_SERVICE__HEADER_FILE__
